package org.inspursc.s0517.health.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.common.converter.PermissionConverter2SysPermission;
import org.inspursc.s0517.health.common.entity.SysPermission;
import org.inspursc.s0517.health.common.enums.ResultEnum;
import org.inspursc.s0517.health.common.exception.BizException;
import org.inspursc.s0517.health.common.form.PermissionForm;
import org.inspursc.s0517.health.common.service.SysPermissionService;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/api/permission")
@Api(value = "菜单操作的Controller")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @ApiOperation(value = "保存菜单信息")
    @PostMapping("/save")
    public ResponseEntity<ResponseVO> save(@ApiParam("菜单表单参数") @Valid PermissionForm permissionForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        SysPermission permission= PermissionConverter2SysPermission.converter(permissionForm);
        SysPermission sysPermission=permissionService.save(permission);
        if(null !=permission){
            return ResponseUtil.success("保存成功",sysPermission);
        }
        return ResponseUtil.error("保存失败",sysPermission);
    }

    @ApiOperation(value = "根据菜单ID，删除菜单信息")
    @GetMapping("/delete/{id}")
    public ResponseEntity<ResponseVO> delete(@ApiParam(value = "菜单ID",required = true) @PathVariable Integer id){
        boolean is=permissionService.deleteById(id);
        if(is){
            return ResponseUtil.success("删除成功",is);
        }
        return ResponseUtil.error("删除失败",is);
    }


    @ApiOperation(value = "根据菜单ID，获取菜单信息")
    @GetMapping("/get/{id}")
    public ResponseEntity<ResponseVO> get(@ApiParam(value = "菜单ID",required = true) @PathVariable Integer id){

        SysPermission permission=permissionService.findById(id);
        if(null != permission){
            return ResponseUtil.success("",permission);
        }
        return ResponseUtil.error("菜单信息为空",permission);
    }


    @ApiOperation(value = "获取所有菜单信息")
    @GetMapping("/getAll")
    public ResponseEntity<ResponseVO> getAll(){
        List<SysPermission> permissionList= permissionService.findAll();
        if(!CollectionUtils.isEmpty(permissionList)){
            return ResponseUtil.success("",permissionList);
        }
        return ResponseUtil.error("菜单信息为空",permissionList);
    }

    @ApiOperation(value = "根据权限组ID，获取菜单信息")
    @GetMapping("/getByGroupId/{groupId}")
    public ResponseEntity<ResponseVO> getByGroupId(@ApiParam(value = "权限组ID",required = true) @PathVariable Integer groupId){
        List<SysPermission> permissionList= permissionService.findByGroupId(groupId);
        if(!CollectionUtils.isEmpty(permissionList)){
            return ResponseUtil.success("",permissionList);
        }
        return ResponseUtil.error("菜单信息为空",permissionList);
    }
}
